Jackson একটি শক্তিশালী এবং বহুল ব্যবহৃত JSON প্রসেসিং লাইব্রেরি যা Java Object এবং JSON এর মধ্যে রূপান্তর সহজ করে। Jackson-এর মূল উপাদানগুলোর মধ্যে রয়েছে ObjectMapper এবং বিভিন্ন Annotations যা JSON প্রসেসিংকে আরও কার্যকর এবং নমনীয় করে তোলে।
1. ObjectMapper এর পরিচিতি
ObjectMapper হল Jackson লাইব্রেরির প্রধান ক্লাস যা Java Object এবং JSON এর মধ্যে রূপান্তরের জন্য ব্যবহৃত হয়।
ObjectMapper এর মূল কার্যক্রম:
- Serialization: Java Object → JSON
- Deserialization: JSON → Java Object
ObjectMapper উদাহরণ
Serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Create a Java object
User user = new User("John Doe", 30);
// Serialize Java object to JSON
String json = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
}
}
class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
Output:
Serialized JSON: {"name":"John Doe","age":30}
Deserialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// JSON string
String json = "{\"name\":\"Jane Doe\",\"age\":25}";
// Deserialize JSON to Java object
User user = mapper.readValue(json, User.class);
System.out.println("Deserialized User: " + user.getName() + ", " + user.getAge());
}
}
Output:
Deserialized User: Jane Doe, 25
2. Jackson Annotations
Jackson বিভিন্ন ধরনের Annotations প্রদান করে, যা JSON প্রসেসিংকে নমনীয় এবং কাস্টমাইজ করার সুযোগ দেয়।
a. @JsonProperty
- ফিল্ড বা মেথডের কাস্টম JSON ফিল্ড নাম সেট করতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty("user_name")
private String name;
@JsonProperty("user_age")
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
Output JSON:
{"user_name":"John Doe","user_age":30}
b. @JsonIgnore
- নির্দিষ্ট ফিল্ডকে সিরিয়ালাইজ বা ডি-সিরিয়ালাইজ থেকে বাদ দিতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
private String name;
@JsonIgnore
private String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
// Getters and setters
}
Output JSON:
{"name":"John Doe"}
c. @JsonIgnoreProperties
- একাধিক ফিল্ডকে একসাথে বাদ দিতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties({"password", "email"})
public class User {
private String name;
private String password;
private String email;
public User(String name, String password, String email) {
this.name = name;
this.password = password;
this.email = email;
}
// Getters and setters
}
Output JSON:
{"name":"John Doe"}
d. @JsonFormat
- Date/Time ফিল্ডের কাস্টম ফরম্যাট সেট করতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class Event {
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date eventDate;
public Event(String name, Date eventDate) {
this.name = name;
this.eventDate = eventDate;
}
// Getters and setters
}
Output JSON:
{"name":"Conference","eventDate":"2023-12-01"}
e. @JsonInclude
- শুধুমাত্র নন-নাল ফিল্ড বা নির্দিষ্ট কন্ডিশনের ফিল্ড JSON-এ রাখতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
}
Output JSON:
{"name":"John Doe"}
f. @JsonCreator এবং @JsonProperty
- কন্সট্রাক্টরের মাধ্যমে JSON ডি-সিরিয়ালাইজ করতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private String name;
private int age;
@JsonCreator
public User(@JsonProperty("name") String name, @JsonProperty("age") int age) {
this.name = name;
this.age = age;
}
}
Input JSON:
{"name":"John Doe","age":30}
g. @JsonManagedReference এবং @JsonBackReference
- Bidirectional সম্পর্ক হ্যান্ডল করতে ব্যবহৃত হয়।
উদাহরণ:
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonBackReference;
import java.util.List;
public class Department {
private String name;
@JsonManagedReference
private List<Employee> employees;
}
public class Employee {
private String name;
@JsonBackReference
private Department department;
}
3. JSON Processing এর সুবিধা
- Serialization & Deserialization: সহজ এবং দ্রুত রূপান্তর।
- Custom Configuration: Annotations ব্যবহার করে কাস্টমাইজেশন।
- Performance: বড় JSON ডেটা প্রসেসিংয়ের জন্য কার্যকর।
- Integration: JPA/Hibernate এবং অন্যান্য ফ্রেমওয়ার্কের সাথে সহজেই ইন্টিগ্রেশন।
Jackson-এর ObjectMapper এবং Annotations JSON প্রসেসিংকে সহজ, দ্রুত এবং নমনীয় করে তোলে। Java Object এবং JSON-এর মধ্যে কাস্টম ম্যাপিং সেটআপ করার জন্য Jackson একটি আদর্শ টুল। Best Practices অনুসরণ করে এবং Common Annotations সঠিকভাবে ব্যবহার করে Jackson-এর পূর্ণ সুবিধা নেয়া সম্ভব।
Read more